﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BingMaps.Models;

namespace BingMaps.Controllers
{
    

    [HandleError]
    public class HomeController : Controller
    {
        private LocationEntities db = new LocationEntities();
        //private OfficeEntities dbOffice = new OfficeEntities();
        
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Locations()
        {
            //Get the data from the database
            //var locations = (from l in db.Locations where l.latitude != 0.0 && l.longitude != 0.0 select l).Take(1000);

            //var locations = (from l in db.vw_LocationMeterData_GeoEncoded select l).Take(13);

            var locations = (from l in db.vw_DisplayActiveAlerts select l).Take(100);

            List<PushPinModel> pushpins = new List<PushPinModel>();
            
            //add info to list of pushpins
            foreach (var location in locations)
            {
                //set the html to pass into the description
                string descriptionHtml = "<p>";

                //descriptionHtml += location.addr_line2;

                if (location.addr_line2 != null)
                    descriptionHtml += "<br />" + location.addr_line2;

                descriptionHtml += string.Format("<br />{0}, {1} {2}</p>", location.addr_city, location.addr_state, location.addr_zip);

                descriptionHtml += string.Format("<b>Phone:</b> {0}<br /><b>Fax:</b> {1}</p>", location.addr_state, location.addr_zip);

                //add the pushpin info
                pushpins.Add(new PushPinModel
                {
                    Description = descriptionHtml,
                    Latitude    = Convert.ToDouble(location.latitude),
                    Longitude   = Convert.ToDouble(location.longitude),
                    Title       = location.addr_line2
                });

            }

            //return the list as JSON
            return Json(pushpins);
        }

        //public ActionResult Offices()
        //{
        //    //Get the data from the database
        //    var locations = dbOffice.Offices;
        //    List<PushPinModel> pushpins = new List<PushPinModel>();

        //    //add info to list of pushpins
        //    foreach (var location in locations)
        //    {
        //        //set the html to pass into the description
        //        string descriptionHtml = "<p>";

        //        descriptionHtml += location.Address1;

        //        if (location.Address2 != null)
        //            descriptionHtml += "<br />" + location.Address2;

        //        descriptionHtml += string.Format("<br />{0}, {1} {2}</p>", location.City, location.State, location.Zip);

        //        descriptionHtml += string.Format("<b>Phone:</b> {0}<br /><b>Fax:</b> {1}</p>", location.State, location.Zip);

        //        //add the pushpin info
        //        pushpins.Add(new PushPinModel
        //        {
        //            Description = descriptionHtml,
        //            Latitude = Convert.ToDouble(location.Latitude),
        //            Longitude = Convert.ToDouble(location.Longitude),
        //            Title = location.OfficeName.ToString()
        //        });
        //    }

        //    //return the list as JSON
        //    return Json(pushpins);
        //}
    }


}
